Abstract
This laboratory has the particular purpose of developing tools for the analysis of the performance of trading activity, from the point of view of the account and from the point of view of the person who operates the account (trader). Therefore, this laboratory is divided into 4 parts, one shows the data used throughout the laboratory while the other 3 parts are each related to the purposes of the complete module. The divisions and specific purposes are as follows:
- Descriptive statistics
- Performance Attribution Measures
- Behavioral Finance
Behavioral finance is the study of the influence of psychology on the behavior of investors or financial analysts. It also includes the subsequent effects on the markets. It focuses on the fact that investors are not always rational, have limits to their self-control, and are influenced by their own biases. Throughout this laboratory a focus in identifying disposition effect, an example of an irrational influence in trading choices is present. A process is followed to find it in historical trading data.
import numpy as np
import pandas as pd
import datetime
import time
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.float_format', '{:.4f}'.format)
from functions import f_pip_size
from data import data_historical, data_pips, data_pips0
from main import historical_data_2,historical_data_3,historical_data_4, capital_evolution, mad, be_fi
from visualizations import g_2, g_1, g_extra_1, g_extra_2, g_extra_3, g_extra_4, g_3
import pandas_datareader as web
import plotly.graph_objects as go
import plotly.express as px
In order to run this notebook, it is necessary to have installed and/or have the requirements.txt file with the following:
The following are the file dependencies that are needed to run this notebook:
data_pips
| TickSize | |
|---|---|
| Instrument | |
| AU200AUD | 1.0000 |
| BCOUSD | 0.0100 |
| CN50USD | 1.0000 |
| CORNUSD | 0.0100 |
| DE10YBEUR | 0.0100 |
| ... | ... |
| XLMUSD | 0.0000 |
| LTCUSD | 0.0100 |
| XMRUSD | 0.0100 |
| XRPUSD | 0.0001 |
| IOTUSD | 0.0001 |
132 rows × 1 columns
data_historical.head(5)
| Time | Position | Symbol | Type | Volume | Price | S / L | T / P | Time.1 | Price.1 | Commission | Swap | Profit | Time3 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-06-08 16:54:29 | 41566202 | LNKUSD | buy | 11.0000 | 23.1984 | NaN | NaN | 2021-06-09 15:26:18 | 24.4500 | 0 | -14.7500 | 1376.8000 | 81109.0000 |
| 1 | 2021-06-08 16:59:03 | 41566725 | BTCUSD | buy | 5.0000 | 32549.5500 | 31000.0000 | 44000.0000 | 2021-06-09 15:26:09 | 35154.7000 | 0 | -93.3200 | 13025.7500 | 80826.0000 |
| 2 | 2021-06-08 16:59:57 | 41566764 | ETHUSD | buy | 5.0000 | 2439.8500 | 2305.7400 | 2750.0000 | 2021-06-09 15:26:15 | 2555.4500 | 0 | -70.3800 | 5780.0000 | 80778.0000 |
| 3 | 2021-06-08 17:00:05 | 41566788 | LTCUSD | buy | 5.0000 | 154.3100 | NaN | NaN | 2021-06-09 15:26:23 | 162.6700 | 0 | -44.8200 | 4180.0000 | 80778.0000 |
| 4 | 2021-06-08 17:00:12 | 41566796 | XLMUSD | buy | 5.0000 | 0.3291 | NaN | NaN | 2021-06-09 15:26:19 | 0.3462 | 0 | -9.7600 | 851.5000 | 80767.0000 |
historical_data_2.head(5)
| Open Time | Position | Symbol | Type | Volume | Open Price | S / L | T / P | Close Time | Close Price | Commission | Swap | Profit | Tiempo | Pips | Pips Accumulated | Profit Accumulated | Profit GP | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-06-08 16:54:29 | 41566202 | LNKUSD | buy | 11.0000 | 23.1984 | NaN | NaN | 2021-06-09 15:26:18 | 24.4500 | 0 | -14.7500 | 1376.8000 | 81109.0000 | 1251.6360 | 1251.6360 | 1376.8000 | 100.0000 |
| 1 | 2021-06-08 16:59:03 | 41566725 | BTCUSD | buy | 5.0000 | 32549.5500 | 31000.0000 | 44000.0000 | 2021-06-09 15:26:09 | 35154.7000 | 0 | -93.3200 | 13025.7500 | 80826.0000 | 260515.0000 | 261766.6360 | 14402.5500 | 90.4406 |
| 2 | 2021-06-08 16:59:57 | 41566764 | ETHUSD | buy | 5.0000 | 2439.8500 | 2305.7400 | 2750.0000 | 2021-06-09 15:26:15 | 2555.4500 | 0 | -70.3800 | 5780.0000 | 80778.0000 | 11560.0000 | 273326.6360 | 20182.5500 | 28.6386 |
| 3 | 2021-06-08 17:00:05 | 41566788 | LTCUSD | buy | 5.0000 | 154.3100 | NaN | NaN | 2021-06-09 15:26:23 | 162.6700 | 0 | -44.8200 | 4180.0000 | 80778.0000 | 836.0000 | 274162.6360 | 24362.5500 | 17.1575 |
| 4 | 2021-06-08 17:00:12 | 41566796 | XLMUSD | buy | 5.0000 | 0.3291 | NaN | NaN | 2021-06-09 15:26:19 | 0.3462 | 0 | -9.7600 | 851.5000 | 80767.0000 | 1703.0000 | 275865.6360 | 25214.0500 | 3.3771 |
historical_data_3.head(5)
| Open Time | Position | Symbol | Type | Volume | Open Price | S / L | T / P | Close Time | Close Price | Commission | Swap | Profit | Tiempo | Pips | Pips Accumulated | Profit Accumulated | Profit GP | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2021-06-08 16:54:29 | 41566202 | LNKUSD | buy | 11.0000 | 23.1984 | NaN | NaN | 2021-06-09 15:26:18 | 24.4500 | 0 | -14.7500 | 1376.8000 | 81109.0000 | 1251.6360 | 1251.6360 | 1376.8000 | 100.0000 |
| 1 | 2021-06-08 16:59:03 | 41566725 | BTCUSD | buy | 5.0000 | 32549.5500 | 31000.0000 | 44000.0000 | 2021-06-09 15:26:09 | 35154.7000 | 0 | -93.3200 | 13025.7500 | 80826.0000 | 260515.0000 | 261766.6360 | 14402.5500 | 90.4406 |
| 2 | 2021-06-08 16:59:57 | 41566764 | ETHUSD | buy | 5.0000 | 2439.8500 | 2305.7400 | 2750.0000 | 2021-06-09 15:26:15 | 2555.4500 | 0 | -70.3800 | 5780.0000 | 80778.0000 | 11560.0000 | 273326.6360 | 20182.5500 | 28.6386 |
| 3 | 2021-06-08 17:00:05 | 41566788 | LTCUSD | buy | 5.0000 | 154.3100 | NaN | NaN | 2021-06-09 15:26:23 | 162.6700 | 0 | -44.8200 | 4180.0000 | 80778.0000 | 836.0000 | 274162.6360 | 24362.5500 | 17.1575 |
| 4 | 2021-06-08 17:00:12 | 41566796 | XLMUSD | buy | 5.0000 | 0.3291 | NaN | NaN | 2021-06-09 15:26:19 | 0.3462 | 0 | -9.7600 | 851.5000 | 80767.0000 | 1703.0000 | 275865.6360 | 25214.0500 | 3.3771 |
g_extra_2(historical_data_3)
g_extra_3(historical_data_3)
Descriptive statistics provide simple summaries about the sample and about the observations that have been made. Such summaries may be either quantitative, i.e. summary statistics, or visual, i.e. simple-to-understand graphs. These summaries may either form the basis of the initial description of the data as part of a more extensive statistical analysis, or they may be sufficient in and of themselves for a particular investigation.
Some measures that are commonly used to describe a data set are measures of central tendency and measures of variability or dispersion. Measures of central tendency include the mean, median and mode, while measures of variability include the standard deviation (or variance), the minimum and maximum values of the variables, kurtosis and skewness.
In this particular case we are measuring the amount of operations, wheter they were or not successful and how successful they were, as a median.
f_pip_size("BTCUSD", data_pips0)
124 100.0000 Name: TickSize, dtype: float64
historical_data_4.get('df_1_tabla')
| Measurment | Value | Description | |
|---|---|---|---|
| 0 | Ops Totales | 17.0000 | Operaciones Totales |
| 1 | Ganadoras | 12.0000 | Operaciones Ganadoras |
| 2 | Ganadoras_c | 12.0000 | Operaciones Ganadoras de Compra |
| 3 | Ganadoras_v | 0.0000 | Operaciones Ganadoras de Venta |
| 4 | Perdedoras | 5.0000 | Operaciones Perdedoras |
| 5 | Perdedoras_c | 5.0000 | Operaciones Perdedoras de Compra |
| 6 | Perdedoras_v | 0.0000 | Operaciones Perdedoras de Venta |
| 7 | Mediana(Profit) | 851.5000 | Mediana de Profit de Operaciones |
| 8 | Mediana(Pips) | 836.0000 | Mediana de Pips de Operaciones |
| 9 | r_efectividad | 0.7059 | Ganadoras Totales/Operaciones Totales |
| 10 | r_proporcion | 2.4000 | Ganadoras Totales/Perdedoras Totales |
| 11 | r_efectividad_c | 0.7059 | Ganadoras Compras/Operaciones Totales |
| 12 | r_efectividad_v | 0.0000 | Ganadoras Ventas/Operaciones Totales |
import plotly.graph_objects as go
g_extra_1(historical_data_4.get('df_1_tabla'))
historical_data_4.get('df_2_ranking')
| Symbol | Rank | |
|---|---|---|
| 1 | ETHUSD | 1.0000 |
| 4 | LNKUSD | 1.0000 |
| 5 | LTCUSD | 1.0000 |
| 7 | XLMUSD | 1.0000 |
| 8 | XMRUSD | 1.0000 |
| 0 | BTCUSD | 0.6667 |
| 9 | XRPUSD | 0.5000 |
| 2 | EURUSD | 0.0000 |
| 3 | IOTUSD | 0.0000 |
| 6 | USDJPY | 0.0000 |
g_1(historical_data_4.get('df_2_ranking'))
Performance attribution, or investment performance attribution is a set of techniques that performance analysts use to explain why a portfolio's performance differed from the benchmark. This difference between the portfolio return and the benchmark return is known as the active return.
The first column, named 'Timestamp' contains the day-by-day dates during the period of trading.
The second column is 'Profit' which is the profit per day for each day of all the contents in the period you traded.
The third column of this DataFrame is 'Accumulated Profit' which is the daily accumulated profit of the account's capital. In particular, the column 'Accumulated Profit' represents the daily capital of the trading account, which before any trading position was closed, started with an amount of 100,000 USD.
capital_evolution
| Profit | Accumulated Profit | |
|---|---|---|
| Close Time | ||
| 2021-06-09 | 31489.0500 | 131489.0500 |
| 2021-06-10 | 28280.0000 | 159769.0500 |
| 2021-06-11 | 0.0000 | 159769.0500 |
| 2021-06-12 | 0.0000 | 159769.0500 |
| 2021-06-13 | 0.0000 | 159769.0500 |
| 2021-06-14 | -0.1200 | 159768.9300 |
| 2021-06-15 | 10439.3600 | 170208.2900 |
| 2021-06-16 | -1223.1000 | 168985.1900 |
| 2021-06-17 | 0.0000 | 168985.1900 |
| 2021-06-18 | 0.0000 | 168985.1900 |
| 2021-06-19 | 0.0000 | 168985.1900 |
| 2021-06-20 | 0.0000 | 168985.1900 |
| 2021-06-21 | -711.0000 | 168274.1900 |
mad
| Metric | Measurment | Value | Description | |
|---|---|---|---|---|
| 0 | Sharpe Original | Amount | 126.8110 | Sharpe Ratio Original Formula |
| 1 | Sharpe Actualizado | Amount | 835.2190 | Sharpe Ratio Adjusted Formula |
| 2 | DrawDown Capital | Initial Date | [2021-06-15 00:00:00] | Initial Date of Capital's DrawDown |
| 3 | DrawDown Capital | Last Date | [2021-06-21 00:00:00] | Last Date of Capital's DrawDown |
| 4 | DrawDown Capital | DrawDown Capital $ | -1934.1000 | Maximum floating loss recorded |
| 5 | DrawUp Capital | Initial Date | [2021-06-09 00:00:00] | Initial Date of Capital's DrawUp |
| 6 | DrawUp Capital | Last Date | [2021-06-15 00:00:00] | Last Date of Capital's DrawUp |
| 7 | DrawUp Capital | DrawUp Capital $ | 38719.2400 | Maximum floating profit recorded |
g_extra_4(mad)
g_2(capital_evolution)
Behavioral economics (also, behavioural economics) studies the effects of psychological, cognitive, emotional, cultural and social factors on the decisions of individuals and institutions and how those decisions vary from those implied by classical economic theory.
Behavioral economics is primarily concerned with the bounds of rationality of economic agents. Behavioral models typically integrate insights from psychology, neuroscience and microeconomic theory. The study of behavioral economics includes how market decisions are made and the mechanisms that drive public choice.
Behavioral finance is the study of the influence of psychology on the behavior of investors or financial analysts. It also includes the subsequent effects on the markets. It focuses on the fact that investors are not always rational, have limits to their self-control, and are influenced by their own biases.
There are 2 approaches to address the existence of this bias,
1) Due to perception error
2) Due to valuation error.
In this lab we will use the 2nd option to build this last part.
be_fi.get('Ocurrencias')
{'Cantidad': 2,
'Ocurrencia 1': {'Timestamp': '06/15/2021, 15:31:44',
'Operaciones': {'Ganadora': {'Instrumento': 'XRPUSD',
'Volumen': 15.0,
'Sentido': 'buy',
'Profit Ganadora': 211.51},
'Perdedora': {'Instrumento': 'BTCUSD',
'Volumen': 5.0,
'Sentido': 'buy',
'Profit Perdedora': -1403.1}},
'ratio_cp_profit_acm': 0.020392312573413632,
'ratio_cg_profit_acm': 0.0066806569539034355,
'ratio_cp_cg': 6.633728901706775},
'Ocurrencia 2': {'Timestamp': '06/15/2021, 15:31:46',
'Operaciones': {'Ganadora': {'Instrumento': 'XLMUSD',
'Volumen': 20.0,
'Sentido': 'buy',
'Profit Ganadora': 120.5},
'Perdedora': {'Instrumento': 'BTCUSD',
'Volumen': 5.0,
'Sentido': 'buy',
'Profit Perdedora': -1403.1}},
'ratio_cp_profit_acm': 0.020392312573413632,
'ratio_cg_profit_acm': 0.0018123418524931207,
'ratio_cp_cg': 11.643983402489626}}
The disposition effect refers to investors’ reluctance to sell assets that have lost value and greater likelihood of selling assets that have made gains. In this particular case it was observed through the historical data. With two cases of strong pressence shown.
be_fi.get('Resultados')
| Ocurrencias | Status Quo % | Aversion Perdida % | Sensibilidad Decreciente | |
|---|---|---|---|---|
| 0 | 2 | 16.6667 | 16.6667 | No |
g_3()
[1] Munnoz, 2020. Python project template. https://github.com/iffranciscome/python-project. (2021).
[2] Unkown. (2015). What is Behavioral Finance?. 06/29/2020, de CFI Sitio web: https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/behavioral-finance/#:~:text=Behavioral%20finance%20is%20the%20study,influenced%20by%20their%20own%20biases.
[3] Shefrin, H., & Statman, M. (1985). The disposition to sell winners too early and ride losers too long: Theory and evidence. The Journal of Finance, 40, 777-790.